<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Html Class - extension to html helpers
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Html
 * @author		Pete Swan
 */
class Html {

    function Html($params = null)
    {
    	if( count($params)){
    		foreach($params as $key => $value){
    			$this->$key = $value;
    		}
    	}
    }
	
	// ------------------------------------------------------------------------
	
	/**
	 * Contains a string in a tag
	 *
	 * @access	public
	 * @param	array
	 * @param	string		
	 * @return	string
	 */	
	function contain_in_element( $element, $string){
		$ret = "";
		if( $string ){
			$ret .= '<' . $element['tag'] . Html::get_attributes($element['attributes']) . '>';
			$ret .= "\n\t$string";
			$ret .= '</' . $element['tag'] . ">\n";
		}else{
			$ret .= '<' . $element['tag'] . Html::get_attributes($element['attributes']) . ' />';
		}
		return $ret;	
	}
	
	// ------------------------------------------------------------------------
	
	function img_tag($attributes, $src){
		return '<img src="' . $src . '" alt="' . $src . '"' . Html::get_attributes($attributes) . ' />';
	}
	
	/**
	 * Generates attribute string
	 *
	 * Generates an attribute string based on key value pairs
	 *
	 * @access	public
	 * @param	array		
	 * @return	string
	 */	
	function get_attributes($attrs)
	{
		$attr_strings = array();
		if( is_array($attrs)){
			foreach( $attrs as $key => $value){
				$attr_strings[] = "$key=\"$value\"";
			}
		}
		return ' ' . join(' ', $attr_strings);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Unordered List
	 *
	 * Generates an HTML unordered list from an single or multi-dimensional array.
	 *
	 * @access	public
	 * @param	array
	 * @param	mixed
	 * @return	string
	 */	
	function ul($list, $attributes = null)
	{
		return Html::_list('ul', $list, $attributes);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Ordered List
	 *
	 * Generates an HTML ordered list from an single or multi-dimensional array.
	 *
	 * @access	public
	 * @param	array
	 * @param	mixed
	 * @return	string
	 */	
	function ol($list, $attributes = null)
	{
		return Html::_list('ol', $list, $attributes);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Generates the list
	 *
	 * Generates an HTML ordered list from an single or multi-dimensional array.
	 *
	 * @access	private
	 * @param	string
	 * @param	mixed		
	 * @param	mixed		
	 * @param	intiger		
	 * @return	string
	 */	
	function _list($type = 'ul', $list, $attributes = null, $depth = 0)
	{
		// If an array wasn't submitted there's nothing to do...
		if ( ! is_array($list))
		{
			return $list;
		}
		
		// Set the indentation based on the depth
		$out = str_repeat(" ", $depth);
		
		// Were any attributes submitted?  If so generate a string
		if (is_array($attributes))
		{
			$atts = Html::get_attributes($attributes);
		}
		else
		{
			$atts = $attributes;
		}
		
		// Write the opening list tag
		$out .= "<".$type.$atts.">\n";
	
		// Cycle through the list elements.  If an array is 
		// encountered we will recursively call _list()
	
		static $_last_list_item = '';
		foreach ($list as $key => $val)
		{	
			$_last_list_item = $key;
	
			$out .= str_repeat(" ", $depth + 2);
			$out .= "<li>";
			
			if ( ! is_array($val))
			{
				$out .= $val;
			}
			else
			{
				$out .= $_last_list_item."\n";
				$out .= Html::_list($type, $val, '', $depth + 4);
				$out .= str_repeat(" ", $depth + 2);
			}
	
			$out .= "</li>\n";		
		}
	
		// Set the indentation for the closing tag
		$out .= str_repeat(" ", $depth);
		
		// Write the closing list tag
		$out .= "</".$type.">\n";
	
		return $out;
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Generate repeating closed element
	 * 
	 * @return string 
	 * @param $element array
	 * @param $times integer
	 */
	function repeating_element($element, $times){
		$str = '<' . $element['tag'] . Html::get_attributes($element['attributes']) . ' />';
		return str_repeat( $str, $times);
	}
}

?>